package com.example.hello_world.okhttp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.net.wifi.aware.DiscoverySession;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.hello_world.R;

import org.json.JSONObject;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class test_okhttp extends AppCompatActivity implements View.OnClickListener {

    private EditText tv;
    private Button getBtn,postBtn;
    private String url="https://www.baidu.com/home/other/data/weatherInfo?city=%E4%B8%9C%E8%8E%9E&indextype=manht&_req_seqid=0x9c6ebec400001301&asyn=1&t=1614846932747&sid=33506_33355_33273_31254_33594_33570_26350_22159";
    private String result,string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_okhttp);
        tv=findViewById(R.id.oktext);
        postBtn=findViewById(R.id.postButton);
        getBtn=findViewById(R.id.getButton);
        getBtn.setOnClickListener(this);
        postBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.getButton:
                gethttp();
                break;
            case R.id.postButton:
                posthttp();
                break;
        }
    }

    public void gethttp() {

        OkHttpClient client=new OkHttpClient().newBuilder().build();
        Request request=new Request.Builder().get().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                result = response.body().string();
                Log.i("成功",result);
                Message message=new Message();
                message.what=1;
                handler1.sendMessage(message);
            }
        });
    }

    private Handler handler1=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    tv.setText(result);
                    break;
                case 2:
                    tv.setText(string);
            }
        }
    };

    public void posthttp() {
        OkHttpClient client=new OkHttpClient().newBuilder().build();
        Map map=new HashMap();
        map.put("jiayou","demoData");
        map.put("nima","demoData");
        JSONObject object=new JSONObject(map);
        String jsonStr = object.toString();
        RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),jsonStr);


        Request request=new Request.Builder()
                .url("https://free-api.heweather.com/s6/weather?location=dongguan&key=26c2a976920d45dab0804d87e52f89c8")
                .addHeader("contentType","application/json;charset=UTF-8")
                .post(requestBody).build();

        Call call=client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                string = response.body().string();
                Log.i("成功2",string);
                Message message=new Message();
                message.what=2;
                handler1.sendMessage(message);
            }
        });
    }
}